package com.klod.news.activity

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.luminance
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.navigation3.rememberViewModelStoreNavEntryDecorator
import androidx.navigation3.runtime.entry
import androidx.navigation3.runtime.entryProvider
import androidx.navigation3.runtime.rememberNavBackStack
import androidx.navigation3.runtime.rememberSavedStateNavEntryDecorator
import androidx.navigation3.ui.NavDisplay
import androidx.navigation3.ui.rememberSceneSetupNavEntryDecorator
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.klod.news.config.RouteError
import com.klod.news.config.RouteMain
import com.klod.news.page.ErrorScreen
import com.klod.news.page.MainScreen
import com.klod.news.ui.theme.NewsAppTheme
import com.klod.news.utils.RouteManage
import com.klod.news.viewmodel.MainActivityVm
import org.koin.androidx.compose.koinViewModel


class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

//        val token = ("这里是显示接收数据的地方" + intent.getStringExtra("token"))
//        token.logE()
//    val backStack = remember { mutableStateListOf<Any>(HomeScreen) }


        setContent {
            NewsAppTheme {
                val systemUiController = rememberSystemUiController()
                val useDarkIcons = MaterialTheme.colorScheme.background.luminance() > 0.5f
                val statusColor = Color.Transparent
                SideEffect {
                    systemUiController.setStatusBarColor(
                        color = statusColor,
                        darkIcons = useDarkIcons
                    )
                }
                //首页内容
//                val context = LocalContext.current
//                val context1:Context by inject()
                val vm = koinViewModel<MainActivityVm>()
                val backStack = rememberNavBackStack(RouteMain)


                vm.routeState.collectAsStateWithLifecycle().value.apply {
                                when (this) {
                                    is RouteManage.AddAll -> {
                                        backStack.clear()
                                        backStack.addAll(routes)
                                    }

                                    is RouteManage.Back -> {

                                        backStack.removeLastOrNull()
                                    }

                                    is RouteManage.Clear -> {
                                        backStack.clear()
                                    }

                                    is RouteManage.JumpTo -> {
                                        backStack.add(route)
                                    }
                                }

                }



                NavDisplay(
                    entryDecorators = listOf(
                        // Add the default decorators for managing scenes and saving state
                        rememberSceneSetupNavEntryDecorator(),
                        rememberSavedStateNavEntryDecorator(),
                        // Then add the view model store decorator
                        rememberViewModelStoreNavEntryDecorator()
                    ),
                    backStack = backStack,
                    onBack = { backStack.removeLastOrNull() },
                    entryProvider = entryProvider {
                        entry<RouteMain> { MainScreen() }
                        entry<RouteError> { ErrorScreen() }

                    }
                )
            }
        }
    }
}

